//
//  HMEmoticonToolBar.swift
//  表情键盘
//
//  Created by WGP on 16/4/20.
//  Copyright © 2016年 WGP. All rights reserved.
//

import UIKit

//1.定义代理协议
protocol HMEmoticonToolBarDelegate: NSObjectProtocol
{
//    2.定义代理方法
    func emoticonToolBarBtnDidClicked(emoticonToolBar: HMEmoticonToolBar, selectedBtnType type:HMEmoticonToolBarType)
}

enum HMEmoticonToolBarType: Int
{
    case Recent = 0
    case Default = 1
    case Emoji = 2
    case Lxh = 3
}

class HMEmoticonToolBar: UIView {
    
//    3.定义代理属性
    weak var delegate: HMEmoticonToolBarDelegate?
    
    private var selectedButton: UIButton?

    class  func emoticonToolBar()->HMEmoticonToolBar {
        
        return NSBundle.mainBundle().loadNibNamed("HMEmoticonToolBar", owner: nil, options: nil).last as! HMEmoticonToolBar
    }
    
    
    @IBAction func didClickButton(sender: UIButton) {

        let type  = HMEmoticonToolBarType(rawValue: sender.tag)!
//        print("\(type)")
        switchSelectedBtn(sender)
        
//        4.调用代理来切换collectionView相应的组
        delegate?.emoticonToolBarBtnDidClicked(self, selectedBtnType:type)
    }
    
//    当移动CollectionView的时候也需要切换选中按钮
    func switchSelectdBtn(section: Int)
    {
        let button = self.subviews[section] as! UIButton
        switchSelectedBtn(button)
    }
    
    private func switchSelectedBtn(sender:UIButton)
    {
        if selectedButton == sender
        {
            return
        }
        selectedButton?.selected = false
        selectedButton = sender
        selectedButton?.selected = true
    }

}
